package myJs.user.workflow

import myJs.Implicits._
import myJs.Utils._
import scala.scalajs.js.Dynamic.{global=>g}
import myJs.myPkg.{LayerOptions, MiniColorsOptions, Select2Options}
import myJs.myPkg.bootstrap.Bootstrap.default._
import myJs.myPkg.jquery._
import myJs.tool.Pojo.PreDealSoft
import myJs.tool.Tool
import myJs.tool.Tool._
import myJs.user.soft.fileSelect.FileModal
import org.scalajs.dom._
import org.scalajs.dom.ext.Ajax
import scalatags.Text.all._
import shared.Pojo._

import scala.scalajs.js
import scala.scalajs.js.annotation.{JSExport, JSExportTopLevel}
import shared.{Pojo, SharedTool}

import scala.scalajs.js.JSON
import scala.scalajs.js.JSConverters._
import org.scalajs.macrotaskexecutor.MacrotaskExecutor.Implicits._

/**
 * Created by yz on 2019/4/25
 */
@JSExportTopLevel("DataPreprocess")
object DataPreprocess extends WorkflowTool {
  val formId = "form"
  val fileModalId = "fileModal"
  val tableId = "table"
  val softModalId = "softModal"
  val insertModalId = "insertModal"

  @JSExport("init")
  def init = {
    FileModal.init
    registerEvent
    $("#form").bv.revalidateField("missionName")

    //    addSoftShow

  }

  def getMissionId = {
    g.missionId.toString
  }

  override def registerEvent = {
    super.registerEvent
    $(document.body).on("click", ".myFileSelect", "", (y: Element) => {
      val name = $(y).dataInputName
      val metDataFileName = Tool.getInputByName("file").myVal
      val lis = $(y).dataValue.split(";")
      $(".myFileSelect").removeClass("myCurrent")
      $(y).addClass("myCurrent")
      FileModal.fileSelectShow(lis)
    })
    $(document.body).on("click", s".myAddSoftShow", "", (y: Element) => {
      addSoftShow
    })
    $(document.body).on("click", s".myRun", "", (y: Element) => {
      myRun
    })
    $(document.body).on("click", s".myRemoveSoft", "", (y: Element) => {
      myRemoveSoft(y)
    })
    $(document.body).on("click", s".myInsertSoftShow", "", (y: Element) => {
      insertSoftShow(y)
    })
    $(document.body).on("change", ".myGroup", "", (y: Element, e: Event) => {
      groupChange(y)
    })
  }

  def groupChange(y: Element) = {
    if (Tool.getInputByName("file").myVal.isEmpty) {
      layer.msg(messages("pleaseFirstSelectDataMatrix"), LayerOptions.icon(5).time(3000))
      $("#form").bv.revalidateField("file")
      $(y).bootstrapToggle("off", true)
    } else if ($(y).isChecked) {
      $("#groupFile").show()
    } else {
      $("#groupFile").hide()
    }
  }

  def insertSoftShow(y: Element) = {
    val index = $(y).parents(".myEachSoft").index()
    $(s"#${insertModalId}").findInputByName("index").`val`(s"${index}")
    $(s"#${insertModalId}").modal("show")
  }

  def clearArrow = {
    $(s".myEachSoft:first-child").find(".myArrow").hide()
  }

  def myRemoveSoft(y: Element) = {
    val jq = $(y).parents(".myEachSoft")
    jq.remove()
    clearArrow
  }

  def addSoftShow = {
    if (Tool.getInputByName("file").myVal.isEmpty) {
      layer.msg(messages("pleaseFirstSelectDataMatrix"), LayerOptions.icon(5).time(3000))
    } else {
      $(s"#${
        softModalId
      }").modal("show")
    }

  }

  def softNumCheck = {
    val size = $(s".myEachSoft").mapElems(y => y).size
    if (size == 0) {
      layer.msg(messages("PleaseAddAtLeastOneModule"), LayerOptions.icon(5).time(3000))
    }
    size > 0
  }

  def getHasGroup = {
    Tool.getInputByName("groupFile").myVal.nonBlank && Tool.getInputByName("hasGroup").
      isChecked
  }

  def myRun = {
    val bv = $("#form").bv
    bv.validate()
    val valid = bv.isValid()
    if (valid && softNumCheck) {
      val element = div(id := "content",
        span(id := "info", messages("running"),
          span(id := "progress", "。。。")), " ",
        img(src := "/assets/images/running2.gif", cls := "runningImage", width := 30, height := 20)
      ).render
      val layerOptions = LayerOptions.title(zhInfo).closeBtn(0).skin("layui-layer-molv").btn(js.Array())
      val index = layer.alert(element, layerOptions)
      val url = g.jsRoutes.controllers.WorkflowController.dataPreprocess().url.toString
      val formData = $(s"#form").serialize()
      Ajax.post(url = s"${
        url
      }", data = s"${
        formData
      }", headers = Map("Content-Type" -> "application/x-www-form-urlencoded")).map {
        xhr =>
          val rs = xhr.responseText.toJDJAn
          layer.close(index)
          val valid = rs("valid").toB
          if (valid) {
            window.location.href = g.jsRoutes.controllers.WorkflowController.missionManageBefore().url.toString
          } else {
            g.swal("Error", rs.myGet("message"), "error")
          }
      }

    }
  }


}
